package com.suning.sawp.service.impl.companymem;

import java.io.InputStream;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;

import com.suning.framework.image.service.ImageServiceClient;
import com.suning.framework.image.service.ImageServiceClientImpl;
import com.suning.framework.image.service.ImageServiceException;
import com.suning.sawp.po.util.OSSResult;

/**
 * UMIG
 * 
 * 集成 “1. 图片处理及存储系统” 苏宁图片处理及存储系统和苏宁内部文件系统是为苏宁各个业务系统提供一个统一的图片处理以及文件存储平台，以提高图片处理及文件存储的方便性和统一性
 * 支持用户只上传原图，系统给用户生成用户想要的尺寸图，并且用户可以访问到各个尺寸图， 缩略图的尺寸需要提前配置在图片处理及存储系统或者内部文件系统中
 * 
 * 只支持对jpg格式
 * 
 * <pre>
 * dev: http://10.19.250.38:81/
 * Sit、Pre: http://10.19.95.100/  ( http://uimgpre.cnsuning.com/)
 * Prd: http://image.suning.cn/
 * http://image1.suning.cn/
 * http://image2.suning.cn/
 * http://image3.suning.cn/
 * http://image4.suning.cn/
 * http://image5.suning.cn/
 * 
 * http://${url}/uimg/${appCode}/${imgType}/${owerId}_${fileIndex}.${fileFix}   (上传时有传fileIndex的场景)
 * 
 * http://${url}/uimg/${appCode}/${imgType}/${owerId}.${fileFix}   (上传时没有传fileIndex的场景)
 * 
 * 图片拼接由暂时层做控制
 * 
 * </pre>
 * 
 * 
 * @author 13071602
 * 
 */
public class SRSuimgStore implements InitializingBean {
    private ImageServiceClient uimg;
    
    @Value("#{settingConfig[uimg_head_host]}")
    String urlHeader;


    private String appCode;

    private String password;

    public void setAppCode(String appCode) {
        this.appCode = appCode;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 返回ownerId,地址由展示层完成拼接,拼接到/uimg/${appCode}/${imgType}/${owerId}.${fileFix}
     * 
     * @param fileType
     * @param fileFix
     * @param file
     * @return
     */
    public OSSResult onlyUpload(String ownerId, String imgIndex, InputStream file) throws ImageServiceException {
        OSSResult oSSResult = new OSSResult();
        uimg.uploadFile(appCode, "certificates", ownerId, imgIndex, file, password);
        oSSResult.setImgUrl(urlHeader +String.format("/uimg/%s/certificates/%s_%s.jpg", this.appCode, ownerId, imgIndex));
        oSSResult.setThumbnailUrl( urlHeader + String.format("/uimg/%s/certificates/%s_%s.jpg?format=690w_690h_0e", this.appCode,
                ownerId, imgIndex, System.currentTimeMillis()));
        return oSSResult;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        uimg = new ImageServiceClientImpl("uimg");
        if (null == this.appCode) {
            this.appCode = "srs";
        }
    }

}
